<?php
/**
 * Real entry of Euso application. This file sets up the environment and fires up ZF.
 *
 * @package Euso
 */

define( 'APPLICATION_NAME', 'Euso' );
define( 'APPLICATION_NAMESPACE', 'Euso' );
define( 'APPLICATION_SLUG', 'euso' );


define( 'CORE_DIRNAME',		APPLICATION_SLUG .'-core' );
define( 'LIB_DIRNAME',		APPLICATION_SLUG .'-lib' );
define( 'MODULE_DIRNAME',	APPLICATION_SLUG .'-modules' );
define( 'DATA_DIRNAME',		APPLICATION_SLUG .'-data' );
define( 'ASSETS_DIRNAME',	APPLICATION_SLUG .'-assets' );
define( 'FILES_DIRNAME',	APPLICATION_SLUG .'-files' );


define( 'ROOT_PATH' , dirname( __FILE__ ) );


define( 'APPLICATION_PATH' , ROOT_PATH .'/'. CORE_DIRNAME );

define( 'CORE_PATH',	ROOT_PATH .'/'. CORE_DIRNAME );
define( 'LIB_PATH' ,	ROOT_PATH .'/'. LIB_DIRNAME );
define( 'MODULE_PATH',	ROOT_PATH .'/'. MODULE_DIRNAME );
define( 'DATA_PATH',	ROOT_PATH .'/'. DATA_DIRNAME );
define( 'ASSETS_PATH',	ROOT_PATH .'/'. ASSETS_DIRNAME );
define( 'FILES_PATH',	ROOT_PATH .'/'. FILES_DIRNAME );


// define the path for config.ini
define( 'CONFIG_PATH', CORE_PATH .'/configs' );


//CHECK: here or index.php?
if (dirname($_SERVER['PHP_SELF']) == '/') {
	define( 'SITE_URL_BASE', '');
} else {
	define( 'SITE_URL_BASE', dirname($_SERVER['PHP_SELF']));
}
if (empty($_SERVER['HTTPS']) === FALSE) {
	define( 'SITE_URL', 'https://'. $_SERVER['SERVER_NAME'] . SITE_URL_BASE );
} else {
	define( 'SITE_URL', 'http://'. $_SERVER['SERVER_NAME'] . SITE_URL_BASE );
}


define( 'ASSETS_URL_BASE', SITE_URL_BASE .'/'. ASSETS_DIRNAME );
define( 'FILES_URL_BASE', SITE_URL_BASE .'/'. FILES_DIRNAME );


// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV',
    (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
    : 'production'));


// set the php include path
set_include_path(
	LIB_PATH . PATH_SEPARATOR .
	ZF_PATH . PATH_SEPARATOR .
	get_include_path()
);


//TODO: these should be helpers

function site_url($sub = NULL)
{
	return SITE_URL . $sub;
}
function site_url_base($sub = NULL)
{
	return SITE_URL_BASE . $sub;
}
function nls2p($str)
{
	return str_replace('<p></p>', '', '<p>'
		. preg_replace('#([\r\n]\s*?[\r\n]){2,}#', '</p>$0<p>', $str)
		. '</p>');
}


//TODO: check for configuration files: if they are not valid, show the message to install


/** Zend_Application */
require_once CORE_PATH .'/Kernel.php';

// Create application
$application = new Kernel(APPLICATION_ENV, CONFIG_PATH . '/config.ini');


// bootstrap, and run
$application->bootstrap()
            ->run();